// 添加其它证据信息
Vue.component('ht-add-chunk-file', {
    name:'ht-add-chunk-file',
    template: `
        <el-dialog title="" :visible.sync="show" 
            :destroy-on-close="true" width="70%" top="20px" @close="close"
            :append-to-body="true" z-index="9999999">
            <el-form label-width="120px" :data="proof" size="mini">
                <el-form-item label="材料名称">
                    <el-input type="text" placeholder="材料名称" v-model="proof.title"></el-input>
                </el-form-item>
                <el-form-item label="材料文件">
                    <el-input type="text" placeholder="材料文件" v-model="proof.path" style="width: 70%"></el-input>
                    <el-button size="mini" @click="addFile" type="primary">点击上传</el-button>
                </el-form-item>
                <el-form-item v-if="percent > 0">
                    <el-progress :text-inside="true" status="success" :stroke-width="20" :percentage="percent"></el-progress>
                </el-form-item>
                <el-form-item label="">
                    <el-button type="primary" @click="doAdd" :disabled="uploadIng || loading" :loading="loading">
                    {{ loading ? '处理中' : '确定'}}
                    </el-button>
                    <el-button type="" @click="close" :disabled="uploadIng || loading">返回</el-button>
                </el-form-item>
            </el-form>
            <input type="file" ref="myfile" @input="fileChange($event)" placeholder="请选择文件" style="display: none">
            
        </el-dialog>
        
    `,
    props: {
        show:{
            type:Boolean,
            default:false
        },
        add_data:{
            type:Object,
            default:function () {
                return {}
            }
        },
        project_item_id:{
            type:Number,
            default:0
        },
        project_id:{
            type:Number,
            default:0
        },
    },
    data() {
        return {
            proof:{},
            tableData:[],
            percent:0,
            is_upload:false,
            uploader:"",
            uploadIng:false,
            loading:false
        }
    },
    created() {
        console.log('addFiles',this.project_item_id)
    },
    mounted(){
        var that = this;
        console.log(WebUploader)
        WebUploader.Uploader.register({
            "before-send-file":"beforeSendFile",//整个文件上传前
            "before-send":"beforeSend",  //每个分片上传前
            "after-send-file":"afterSendFile",  //分片上传完毕
        },{
            beforeSend: function (block) {
                if (that.is_upload) {
                    var deferred = WebUploader.Deferred();
                    $.ajax({
                        type: "GET",
                        url: "/admin/chunk/checkChunk",
                        data: {
                            //文件唯一标记
                            md5: that.fileMd5,
                            //当前分块下标
                            chunk: block.chunk,
                            //当前分块大小
                            chunkSize: block.end - block.start
                        },
                        dataType: "json",
                        success: function (res) {
                            if (res.code == 1) {
                                //分块存在，跳过
                                console.log(block.chunk + "reject");
                                deferred.reject();
                            } else {
                                //分块不存在或不完整，重新发送该分块内容
                                console.log(block.chunk + "resolve");
                                deferred.resolve();
                            }
                        }
                    });
                    return deferred.promise();
                }
            }
        });
        that.uploader = WebUploader.create({

            // swf文件路径
            swf: '/static/webuploader/Uploader.swf',

            // 文件接收服务端。
            server: '/admin/chunk/chunkUpload',
            extensions: 'jpg,png,bmp,gif,rar,zip,gz,mp4,avi,mp3,mov,flv,mpeg,doc,docx,pdf,xls,xlsx,ppt,pptx',
            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            // pick: {
            //     id: "#upload",
            // },
            formData: {
            },
            chunked: true, //是否要分片处理大文件上传
            chunkSize: 5*1024*1024, //分片上传，每片2M，默认是5M
            // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            resize: false
        });
        that.uploadInit()
    },
    methods: {
        addFile() {
            this.$refs.myfile.dispatchEvent(new MouseEvent('click'));
        },
        onSuccess(res,file) {
            let vs = this
            if (res.code == 1) {
                vs.proof = {...vs.proof,...res.data}
            } else {
                this.$message.error(res.msg)
            }
            console.log(vs.proof)
        },
        doAdd(){
            vs.loading = true
            let vs = this
            let url = "/admin/lawyer.project_files/add"
            vs.proof.project_item_id = vs.project_item_id
            vs.proof.project_id = vs.project_id
            vs.proof.project_type = "other"
            if (vs.proof.id) {
                url = "/admin/lawyer.project_files/edit"
            }
            Api.request(url,vs.proof).then(res=>{
                vm.showSuccessMsgCall(()=>{
                    vs.$emit('update:show',false)
                    this.$emit('close')
                    this.$emit('success')
                })
            }).finally(()=>{
                vs.loading = false
            })
        },
        close(){
            var that = this ;
            that.uploader.stop()
            // that.$emit('update:show',false)
            that.$emit('close')
            console.log('z close')
        },
        fileChange(e) {
            this.uploader.reset();
            console.log(e.target.files[0])
            this.uploader.addFiles(e.target.files[0])
        },
        uploadInit(){
            var load ;
            var that = this ;
            let vs = this;
            console.log("init",this.uploader);
            that.uploader.on('fileQueued', function( file ) {
                that.percent = 0;
                that.path = '';
                load = layer.load(1,{
                    shade: [0.1,'#000']
                });
                that.uploader.md5File(file,0,10*1024*1024)
                    .progress(function (percentage) {
                        $("#path").val("获取文件信息中……"+(percentage * 100).toFixed(2) + '%');
                    })
                    .then(function (val) {
                        $("#path").val(file.name + " 等待上传");
                        layer.closeAll();
                        var formData = that.uploader.option("formData");
                        formData.md5 = val;
                        formData.ext = file.ext;
                        that.uploader.option("formData",formData);
                        that.fileMd5 = val;
                        that.ext = file.ext;
                        // $.post('/admin/chunk/checkMd5',{md5:val}, function (res) {
                        //     if (res.code == 1) { // 已经上传过
                        //         that.is_upload = "true"
                        //     }  else {
                        //         that.is_upload = "false"
                        //     }
                        // })
                        that.uploader.upload()

                    })
            });
            that.uploader.on('uploadProgress', function( file, percentage ) {
                that.percent = parseInt(percentage * 100);
                if (that.percent < 100) {
                    that.uploadIng = true
                } else {
                    that.uploadIng = false
                }
            });

            that.uploader.on('uploadSuccess', function (file,res) {
                console.log(file,res)
                layer.msg("处理中",{shade: [0.1,'#000'],icon:16,time:9999999});
                $.post("/admin/lawyer.project_files/mergeChunk",{md5:that.fileMd5,ext:that.ext,type:file.type,name:file.name,size:file.size}, function (res1) {
                    layer.closeAll();
                    if (res1.code === 1) {
                        layer.msg("上传成功",{time:500},function (){
                            that.proof = {...that.proof,...res1.data}
                            console.log(vs.proof)
                            that.percent = 0
                        });
                    } else  {
                        this.$message.error(res.msg)
                    }
                })
            });
            that.uploader.on('uploadAccept',function (file,res) {
                if (res.code == 0) {
                    layer.alert(res.msg)
                    return false;
                }
            });
        },
        close1(){
            console.log("temp",this.path);
            this.$emit('close',this.path)
        },
    },
    watch: {
        itemData(v) {
            console.log('son',v)
            if (v && typeof v.proof === "object") {
                this.proof = v.proof;
                console.log('proof',this.proof)
            }
        },
        add_data(v) {
            console.log('add_data',v)
            if (this.add_data.path) {
                this.proof = this.add_data
            }
        }
    }
})